package com.wiim.adapter;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.wiim.connector.RestClient;
import com.wiim.model.ExchangeAwardData;
import com.wiim.nhapnhap.CustomFragmentActivity;
import com.wiim.nhapnhap.R;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExchangeLstAdapter extends ArrayAdapter<ExchangeAwardData> {

	public ArrayList<ExchangeAwardData> mApps;
	private FragmentActivity context;
	private Map<String, byte[]> mIcons;
	private Drawable mStdImg;
//	private NumberFormat formatter = new DecimalFormat("#,###,###");
	private String service_url = "common/getfilestream/%s";
	
//	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	//protected ImageLoader imageLoader = ImageLoader.getInstance();
	
//	private DisplayImageOptions options;	
	
//	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {
//
//		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());
//
//		@Override
//		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
//			if (loadedImage != null) {
//				ImageView imageView = (ImageView) view;
//				boolean firstDisplay = !displayedImages.contains(imageUri);
//				if (firstDisplay) {
//					FadeInBitmapDisplayer.animate(imageView, 500);
//					displayedImages.add(imageUri);
//				}
//			}
//		}
//	}

	public ExchangeLstAdapter(FragmentActivity _context, int textViewResourceId, ArrayList<ExchangeAwardData> mApps) {
		super(_context, textViewResourceId, mApps);
		this.mApps = new ArrayList<ExchangeAwardData>();
		this.mApps.addAll(mApps);
		this.context = _context;
		mStdImg = context.getResources().getDrawable(R.drawable.no_image);
		
//		((CustomFragmentActivity)context).imageLoader2.init(ImageLoaderConfiguration.createDefault(context));
//		options = new DisplayImageOptions.Builder()
//		//.showImageOnLoading(R.drawable.ic_stub)
//		.showImageForEmptyUri(R.drawable.ic_empty)
//		.showImageOnFail(R.drawable.ic_error)
//		.cacheInMemory()
//		.cacheOnDisc()
//		.imageScaleType(ImageScaleType.EXACTLY)
//		.bitmapConfig(Bitmap.Config.RGB_565)
//		.displayer(new RoundedBitmapDisplayer(20))
//		.build();
	}
	
	public void updateNewAdsModelList(ArrayList<ExchangeAwardData> mApps){
		this.mApps.clear();
		this.mApps.addAll(mApps);
		this.notifyDataSetChanged();
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		AppViewHolder holder;
		if (convertView == null) {
			holder = new AppViewHolder();
			LayoutInflater inflater = LayoutInflater.from(context);
			convertView = inflater.inflate(R.layout.doithuong_item, null);
			holder.mTitle = (TextView) convertView.findViewById(R.id.tvDetail);
			holder.mIcon = (ImageView) convertView.findViewById(R.id.imgPics);
			
			convertView.setTag(holder);
		} else {
			holder = (AppViewHolder) convertView.getTag();
		}
		try {			
			Log.e("ListAdAdapter Log:", String.valueOf(position));
			
			final ExchangeAwardData app = mApps.get(position);
			
			// set image
//			if (mIcons == null || mIcons.get(app.getAdsid()) == null) {
//				holder.setIcon(mStdImg);
//			} else {				
//				InputStream inputStream = new ByteArrayInputStream(mIcons.get(app.getAdsid()));
//				Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
//				holder.mIcon.setImageBitmap(ALTSysUtils.getRoundedBitmap(bitmap, 40));
//				bitmap.recycle();
//			}
									
			holder.setTitle(app.ShopName);
			
			convertView.setOnClickListener(new OnClickListener() {

				public void onClick(View v) {
					
//					((CustomFragmentActivity)context).imageLoader2.clearMemoryCache();
//					((CustomFragmentActivity)context).imageLoader2.clearDiscCache();
//					((CustomFragmentActivity)context).imageLoader2.stop();
					
				}
			});			
			
			((CustomFragmentActivity)context).imageLoader2.displayImage(RestClient.apiUrl(format(service_url, app.ShopID)), holder.mIcon, ((CustomFragmentActivity) context).options);

		} catch (Exception e) {
			e.printStackTrace();
		}
		return convertView;
	}

	public static String format(String formatText, Object param0) {
		return format(formatText, new Object[] { param0 });
	}

	@Override
	public int getCount() {
		return mApps.size();
	}

	public ExchangeAwardData getItem(int position) {
		return mApps.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public void setIcons(Map<String, byte[]> icons) {
		this.mIcons = icons;
	}

	public class AppViewHolder {

		private TextView mTitle;		
		private ImageView mIcon;

		public void setTitle(String title) {
			mTitle.setText(title);
		}

		public TextView getTitle() {
			return mTitle;
		}
		
		public void setIcon(Drawable img) {			
				mIcon.setImageDrawable(img);		
		}		
		
		public ImageView getIcon() {
				return mIcon;
		}	

	}
}
